package org.summer.lawyer.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.summer.common.core.utils.MapstructUtils;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.lawyer.domain.Feedback;
import org.summer.lawyer.domain.vo.feedback.FeedbackCreateVo;
import org.summer.lawyer.domain.vo.feedback.FeedbackUpdateVo;
import org.summer.lawyer.domain.vo.feedback.FeedbackVo;
import org.summer.lawyer.mapper.FeedbackMapper;
import org.summer.lawyer.service.IFeedbackService;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 意见反馈Service业务层处理
 *
 * @author summer
 */
@RequiredArgsConstructor
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback>  implements IFeedbackService {


    /**
     * 查询意见反馈
     *
     * @param id 主键
     * @return 意见反馈
     */
    @Override
    public FeedbackVo selectFeedbackVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询意见反馈列表
     *
     * @param vo        查询条件
     * @param pageQuery 分页参数
     * @return 意见反馈分页列表
     */
    @Override
    public TableDataInfo<FeedbackVo> selectPageList(FeedbackVo vo, PageQuery pageQuery) {
        LambdaQueryWrapper<Feedback> lqw = buildQueryWrapper(vo);
        lqw.orderByDesc(Feedback::getId);
        Page<FeedbackVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的意见反馈列表
     *
     * @param vo 查询条件
     * @return 意见反馈列表
     */
    @Override
    public List<FeedbackVo> selectFeedbackVoList(FeedbackVo vo) {
        LambdaQueryWrapper<Feedback> lqw = Wrappers.lambdaQuery();
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Feedback> buildQueryWrapper(FeedbackVo vo) {
        Map<String, Object> params = vo.getParams();
        LambdaQueryWrapper<Feedback> lqw = Wrappers.lambdaQuery();
       // lqw.eq(vo.getWxUserId() != null, Feedback::getWxUserId, vo.getWxUserId());
       // lqw.like(StrUtil.isNotBlank(vo.getNickname()), Feedback::getNickname, vo.getNickname());
        //lqw.eq(StrUtil.isNotBlank(vo.getContactPhone()), Feedback::getContactPhone, vo.getContactPhone());
        lqw.eq(StrUtil.isNotBlank(vo.getFeedbackDetail()), Feedback::getFeedbackDetail, vo.getFeedbackDetail())
                .between(params.get("beginTime") != null && params.get("endTime") != null, Feedback::getFeedbackTime, params.get("beginTime"), params.get("endTime"));
        return lqw;
    }

    /**
     * 新增意见反馈
     *
     * @param vo 意见反馈
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByVo(FeedbackCreateVo vo) {
        Feedback add = MapstructUtils.convert(vo, Feedback.class);
        validEntityBeforeSave(add);
        add.setFeedbackTime(DateUtil.date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            vo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改意见反馈
     *
     * @param vo 意见反馈
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByVo(FeedbackUpdateVo vo) {
        Feedback update = MapstructUtils.convert(vo, Feedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Feedback entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除意见反馈信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
